﻿using suirui.ZhuMu.Log;
using suirui.ZhuMu.Model;
using suirui.ZhuMu.Entity;
using suirui.ZhuMu.Entity.Entities;
using suirui.ZhuMu.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Web;
using suirui.ZhuMu.Common.Helper;
using System.Linq.Expressions;
using Newtonsoft.Json;
using Microsoft.EntityFrameworkCore;

namespace suirui.ZhuMu.Repositories
{
    public class UserRepository : BaseRepository<User>, IUserRepository
    {
        public mjgamesContext _context { get; set; }

        public UserRepository(mjgamesContext context)
            : base(context)
        {
            this._context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public async Task<PageData<User>> GetUserList(GetListByUserDto paramsDto)
        {

            Expression<Func<User, bool>> whereLambda =
                t => t.Role != 0 &&
                t.Status != 0;

            Expression<Func<User, bool>> where = null;
            Expression<Func<User, bool>> expression = whereLambda;
            if (paramsDto.Role > 0)
            {
                where = t => t.Role == paramsDto.Role;
                expression = ExpressionHelp.And(whereLambda, where);
            }

            int pageSize = int.MaxValue;

            var result = await GetPageAsync(expression, t => t.CreateTime, paramsDto.PageIndex, paramsDto.PageSize, false);
            return result;
        }

        public async Task<bool> AddUser(User user)
        {
            var defaultPassword = ConfigurationHelper.GetConfigValueByKey("DefaultPassword");
            user.Password = DESUtils.EncryptString(defaultPassword);

            user.UserId = Snowflake.Instance.nextId();
            user.CreateTime = DateTime.Now;
            user.Status = 1;

            return await InsertAsync(user);
        }

        public async Task<bool> UpdateUser(User user)
        {
            var _user = await GetAsync(t => t.UserId == user.UserId);
            return false;
        }

    }
}
